package com.mmd.redis;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.JedisPubSub;

import java.util.*;

/**
 * @Author cyq
 * @Date 2019/8/21 22:21
 * @Description
 **/

public class RedisManager {
    private static Logger log = LoggerFactory.getLogger(RedisManager.class);
    public static int USER_SESSION_TIME = 864000;
    public static int EXP_ONE_DAY = 86400;
    public static int FIFTEEN_DAY = 1296000;
    public static int EXP_ONE_HOURS = 7200;

    public RedisManager() {
    }

    public static int stringToAscii(String value) {
        char[] chars = value.toCharArray();
        int randomNumber = 0;

        for(int i = 0; i < chars.length; ++i) {
            randomNumber += chars[i];
        }

        return Integer.valueOf(randomNumber) % 10 % 10;
    }

    public static void set(String key, Object val) {
        set(key, val, 0);
    }

    public static void set(String key, Object val, int exptime) {
        SessionCacheManager.inst().set(key, val, exptime);
    }

    public static void setUnserialize(String key, Object val) {
        setUnserialize(key, val, 0);
    }

    public static void setUnserialize(String key, Object val, int exptime) {
        SessionCacheManager.inst().setUnserialize(key, val, exptime);
    }

    public static void setExpire(String key, int exptime) {
        SessionCacheManager.inst().expire(key, exptime);
    }

    public static int getInt(String key) {
        Object v = get(key);
        return v == null ? 0 : (Integer)v;
    }

    public static Object get(String key) {
        try {
            return SessionCacheManager.inst().get(key) != null && !SessionCacheManager.inst().get(key).equals("null") ? SessionCacheManager.inst().get(key) : null;
        } catch (Exception var2) {
            log.error("提取对象缓存:{},{}", SessionCacheManager.inst(), key);
            log.error("错误信息", var2);
            return null;
        }
    }

    public static void delete(String key) {
        SessionCacheManager.inst().delete(key);
    }

    public static void zdelete(String key, String member) {
        SessionCacheManager.inst().zdelete(key, member);
    }

    public static void zadd(String key, double sn, String uid) {
        zadd(key, sn, uid, 0);
    }

    public static void zadd(String key, double sn, String member, int exptime) {
        SessionCacheManager.inst().zincrby(key, sn, member, exptime);
    }

    public static void sadd(String key, String member, int exptime) {
        SessionCacheManager.inst().sadd(key, member, exptime);
    }

    public static void sadd(int exptime, String key, String... members) {
        SessionCacheManager.inst().sadd(exptime, key, members);
    }

    public static void srem(String key, String member) {
        SessionCacheManager.inst().srem(key, new String[]{member});
    }

    public static Set zrevrank(String key, int pi, int pageSize) {
        return SessionCacheManager.inst().zrevrank(key, pi, pageSize);
    }

    public static Long zrevrank(String key, String member) {
        return SessionCacheManager.inst().zrevrank(key, member);
    }

    public static Set<String> smembers(String key) {
        return SessionCacheManager.inst().smembers(key);
    }

    public static void zset(String key, long gold, String uid) {
        zset(key, (double)gold, uid, 0);
    }

    public static void zset(String key, double gold, String uid, int exptime) {
        SessionCacheManager.inst().zadd(key, gold, uid, exptime);
    }

    public static void setList(String key, Object value, int exptime) {
        SessionCacheManager.inst().setList(key, value, exptime);
    }

    public static Long lpush(String key, String value) {
        return SessionCacheManager.inst().lpush(key, value);
    }

    public static Long rpush(String key, String value) {
        return SessionCacheManager.inst().rpush(key, value);
    }

    public static Long getListLen(String key) {
        return SessionCacheManager.inst().getListLen(key);
    }

    public static List<String> brpop(String key, int timeout) {
        return SessionCacheManager.inst().brpop(key, timeout);
    }

    public static List<String> blpop(String key, int timeout) {
        return SessionCacheManager.inst().blpop(key, timeout);
    }

    public static Long ttl(String key) {
        return SessionCacheManager.inst().ttl(key);
    }

    public static void setList(String key, Object value) {
        SessionCacheManager.inst().setList(key, value);
    }

    public static Object getLeftList(String key) {
        return SessionCacheManager.inst().getListL(key);
    }

    public static long getLLen(String key) {
        return SessionCacheManager.inst().getListLen(key);
    }

    public static Object getListHead(String key) {
        return SessionCacheManager.inst().getLpop(key);
    }

    public static Object getListTail(String key) {
        return SessionCacheManager.inst().getRpop(key);
    }

    public static void ldelete(String key, Object value, int count) {
        SessionCacheManager.inst().ldelete(key, value, count);
    }

    public static Object lrpop(String key) {
        return SessionCacheManager.inst().rpop(key);
    }

    public static Double get(String key, String uid) {
        Double v = SessionCacheManager.inst().zscore(key, uid);
        return v == null ? -1.0D : v;
    }

    public static Double getHaveNull(String key, String uid) {
        Double v = SessionCacheManager.inst().zscore(key, uid);
        return v;
    }

    public static <T> List<T> getList(String key) {
        return SessionCacheManager.inst().getListByType(key);
    }

    public static List<Object> getList(String key, int start, int end) {
        return SessionCacheManager.inst().getList(key, start, end);
    }

    public static List<String> sort(String key, int start, int end) {
        List<String> list = new ArrayList();
        Set set = zrevrank(key, start, end);
        Object[] objs = set.toArray();

        for(int i = 0; i < set.size(); ++i) {
            byte[] val = (byte[])((byte[])objs[i]);
            String uid = new String(val);
            list.add(uid);
        }

        return list;
    }

    public static long getSize(String key) {
        return SessionCacheManager.inst().getSize(key);
    }

    public static void hmset(String key, Map map) {
        SessionCacheManager.inst().hmset(key, map);
    }

    public static void hset(String key, String field, String value) {
        SessionCacheManager.inst().hset(key, field, value);
    }

    public static String hget(String key, String field) {
        return SessionCacheManager.inst().hget(key, field);
    }

    public static int hlen(String key) {
        return SessionCacheManager.inst().hlen(key);
    }

    public static boolean hexists(String key, String field) {
        return SessionCacheManager.inst().hexists(key, field);
    }

    public static Set<String> hkeys(String key) {
        return SessionCacheManager.inst().hkeys(key);
    }

    public static boolean sismember(String key, String uid) {
        return SessionCacheManager.inst().sismember(key, uid);
    }

    public static void hincrby(String key, String field, long num, int exptime) {
        if (!StringUtils.isEmpty(field)) {
            SessionCacheManager.inst().hincrby(key, field, num, exptime);
        }
    }

    public static Long incrby(String key, long num, int exptime) {
        return StringUtils.isEmpty(key) ? null : SessionCacheManager.inst().incrby(key, num, exptime);
    }

    public static Boolean exists(String key) {
        return StringUtils.isEmpty(key) ? false : SessionCacheManager.inst().exists(key);
    }

    public static Long reIncrby(String key, long num, int exptime) {
        return StringUtils.isEmpty(key) ? 0L : SessionCacheManager.inst().reIncrby(key, num, exptime);
    }

    public static Long reIncr(String key, int exptime) {
        return StringUtils.isEmpty(key) ? 0L : SessionCacheManager.inst().reIncr(key, exptime);
    }

    public static Long setnx(String key, Object value) {
        return SessionCacheManager.inst().setnx(key, value);
    }

    public static Object getSet(String key, Object value) {
        return SessionCacheManager.inst().getSet(key, value);
    }

    public static Long publish(String channel, String message) {
        return SessionCacheManager.inst().publish(channel, message);
    }

    public static void subscribe(JedisPubSub jedisPubSub, String... channels) {
        SessionCacheManager.inst().subscribe(jedisPubSub, channels);
    }

    public static Set<String> sinter(String... keys) {
        return SessionCacheManager.inst().sinter(keys);
    }

    public static void batchDelete(String key) {
        SessionCacheManager.inst().batchDelete(key);
    }

    public Long geoadd(String key, GeoCoordinate coordinate, String memberName) {
        return SessionCacheManager.inst().geoadd(key, coordinate, memberName);
    }

    public List<GeoRadiusResponse> geoRadius(String key, GeoCoordinate coordinate, double radius) {
        return SessionCacheManager.inst().geoRadius(key, coordinate, radius);
    }

    List<GeoRadiusResponse> georadiusByMember(String key, String member, double radius) {
        return SessionCacheManager.inst().georadiusByMember(key, member, radius);
    }

    public Double geoDist(String key, String member1, String member2, GeoUnit unit) {
        return SessionCacheManager.inst().geoDist(key, member1, member2, unit);
    }

    public List<String> geohash(String key, String... members) {
        return SessionCacheManager.inst().geohash(key, members);
    }

    public List<GeoCoordinate> geopos(String key, String... members) {
        return SessionCacheManager.inst().geopos(key, members);
    }

    public static int getLastTimeToDay() {
        Calendar cal = Calendar.getInstance();
        cal.set(11, 24);
        cal.set(13, 0);
        cal.set(12, 0);
        cal.set(14, 0);
        return (int)((cal.getTimeInMillis() - (new Date()).getTime()) / 1000L);
    }

    public static int getLastSecondsToMonthEnd() {
        Calendar cal = Calendar.getInstance();
        cal.add(2, 1);
        cal.set(5, 1);
        cal.set(11, 0);
        cal.set(13, 0);
        cal.set(12, 0);
        cal.set(14, 0);
        return (int)(cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }

    public static int getLastTimeToTimePoint(int timepoint) {
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(11);
        int minute = cal.get(12);
        int second = cal.get(13);
        int s = 0;
        if (hour < timepoint) {
            s = (timepoint - hour) * 3600;
        } else {
            s = (24 + timepoint - hour) * 3600;
        }

        s = s - minute * 60 - second;
        return s;
    }
}